//
//  WeatherModel.m
//  QuickCure
//
//  Created by Kent on 16/9/14.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "WeatherModel.h"
#import "NetWorkTool.h"

/* 百度天气 */
#define kBDWeather_KEY @"17IvzuqmKrK1cGwNL6VQebF9"
#define kGetWeather_URL(city) ([NSString stringWithFormat: @"http://api.map.baidu.com/telematics/v3/weather?location=%@&output=json&ak=%@", city, kBDWeather_KEY])

@interface WeatherModel ()



@end

@implementation WeatherModel

-(NSArray *)results {
    if (!_results) {
        _results = [NSArray array];
    }
    return _results;
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}

+(instancetype)weatherWithDictionary:(NSDictionary *)dict {
    
    WeatherModel *model = [[WeatherModel alloc]init];
    
    [model setValuesForKeysWithDictionary:dict];
    
    return model;
    
}

-(void)getWeatherWithCityName:(NSString *)cityName SuccessBlock:(void (^)(NSString *city, NSDictionary *currentWeather, NSString *pm25q))successBlock errorBlock:(void (^)())errorBlock {
    
    NSString *url = @"http://api.map.baidu.com/telematics/v3/weather";
    
    NSDictionary *paramDic = @{@"location":cityName,
                               @"output":@"json",
                               @"ak":@"17IvzuqmKrK1cGwNL6VQebF9"
                               };
    
    [[NetWorkTool shareManager] GET:url parameters:paramDic progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *resultDict = [responseObject[@"results"] firstObject];
        
        NSString *city = resultDict[@"currentCity"];
        
        NSString *quality = resultDict[@"pm25"];
        
        NSDictionary *currentWeather = [resultDict[@"weather_data"] firstObject];;
        
        if (successBlock) {
            successBlock(city,currentWeather,quality);
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (errorBlock) {
            errorBlock();
        }
        
    }];
    
    
}

@end





































